package com.monkeyboy.service.rockettransaction;

import com.alibaba.fastjson.JSON;
import org.apache.rocketmq.client.producer.TransactionSendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @Author Gavin
 * @date 2020.08.22 11:05
 */
@Service
public class OrderService {
    @Autowired
    private OrderTransactionListener orderTransactionListener;
    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    public void createOrder() throws Exception {
        //组装订单信息
        HashMap<String, Object> orderInfo = new HashMap<>();
        orderInfo.put("orderId", UUID.randomUUID().toString());
        orderInfo.put("goodsId", UUID.randomUUID().toString());
        orderInfo.put("goods_num", 1);
        orderInfo.put("price", 200);
        Message<HashMap<String, Object>> msg = MessageBuilder.withPayload(orderInfo).build();
        Map<String, String> args = new HashMap<>();
        args.put("args", "额外传递的参数");
        TransactionSendResult result = rocketMQTemplate.sendMessageInTransaction("tx_order_topic", msg, args);
        System.out.println(result);
    }
}
